<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<header th:replace="admin/header::header-fragment">
</header>
<body>
<shiro:user>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <shiro:hasPermission name="sys:pl:delete"><button class="layui-btn layuiadmin-btn-comm" data-type="batchdel">删除</button></shiro:hasPermission>
          <shiro:hasPermission name="sys:pl:delete"><button class="layui-btn layuiadmin-btn-comm" data-type="add">添加公告</button></shiro:hasPermission>
        </div>
        <table id="LAY-app-content-comm" lay-filter="LAY-app-content-comm" class="demo"></table>
        <shiro:hasPermission name="sys:pl:update"><script type="text/html" id="layuiadmin-app-cont-tagsbar1">
          <input type="checkbox"  name="open" value="{{d.commentId}}" lay-skin="switch" lay-filter="Status" lay-text="已审核|未审核" {{ d.commentStatus == 1 ? 'checked' : '' }}>
        </script></shiro:hasPermission>
        <shiro:hasPermission name="sys:pl:update">
          <script type="text/html" id="table-content-com-update">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        </script>
        </shiro:hasPermission>
        <shiro:hasPermission name="sys:pl:delete">
        <script type="text/html" id="table-content-com">
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script></shiro:hasPermission>
      </div>
    </div>
  </div>
</shiro:user>
  <shiro:guest>
    <footer th:replace="admin/nologin::header-fragment">
    </footer>
  </shiro:guest>
  <script src="layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: 'layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'contlist', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table;
    
    
    //监听搜索
    form.on('submit(LAY-app-contcomm-search)', function(data){
      var field = data.field;
    });

    table.render({
      elem: '.demo'
      ,url: 'admin/allgonggao',
      page: true //开启分页
      ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
      ,defaultToolbar:false

      ,title: '用户数据表'
      ,cols: [
        [
          {type: 'checkbox',width:40},
          {field:'blogGgid', title:'公告ID', width:80,align:'center',unresize:true},
          {field:'blogGgtitle', title:'公告标题', width:180,align:'center',unresize:true},
          {field:'blogGgcontent', title:'公告内容', width: 255,unresize:true},
          {field:'blogCreatetime', title:'公告时间', width:180,align:'center',unresize:true,templet: "<div>{{layui.util.toDateString(d.blogCreatetime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
          {field:'right', title:'操作',toolbar:'#table-content-com-update',unresize:true, align:'center',width: 90},
          {field:'right', title:'操作',toolbar:'#table-content-com',unresize:true, align:'center',width: 90}
        ]
      ]
      ,limits: [5,10,20,50]  //每页条数的选择项，默认：[10,20,30,40,50,60,70,80,90]。
      ,limit: 5 //每页默认显示的数量
      ,parseData: function(res) { //res 即为原始返回的数据
        return {
          "code": 0, //解析接口状态
          "msg": "", //解析提示文本
          "count": res.count,
          data: res.data.items //解析数据列表
        };
      }
    });

    form.on('switch(Status)', function(obj){
      var commentId = this.value;
      var commentStatus = obj.elem.checked?1:0;
      //请求接口
      $.post('admin/updateCommentStatus',{
        "commentId":commentId,
        "commentStatus":commentStatus
      },'json');
    });
    //点击事件
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-app-content-comm')
        ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }
      
        layer.confirm('确定删除吗？', function(index) {
          $.ajax({
            url:'admin/deleteGg',
            data:{"delete":JSON.stringify(checkData)},
            dataType:'json',
            method:'post',
            success:function (res) {
              if (res.success){
                layer.msg('删除成功', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }else {
                layer.msg('删除失败', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }
              table.reload('LAY-app-content-comm');
            }
            ,error: function(){
              layer.alert('系统错误，请联系管理员！', {
                icon: 2,
                skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
              });
            }
          });

          table.reload('LAY-app-content-comm');
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加公告'
          ,content: 'admin/addGonggao'
          ,area: ['440px', '320px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
            var body = layer.getChildFrame('body', index);
            var blogGgtitle = body.find('input[name="blogGgtitle"]').val()
            var blogGgcontent = body.find('textarea[name="blogGgcontent"]').val()
            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              $.post('admin/addGonggaos',{
                "blogGgtitle":blogGgtitle,"blogGgcontent":blogGgcontent
              },function (res){
                if (res.success){
                  layer.msg('添加公告成功', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }else {
                  layer.msg('添加公告失败', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }
                table.reload('LAY-app-content-comm');
              },'json');
              layer.close(index); //关闭弹层
            });

            submit.trigger('click');
          }
        });
      }
    }

    //监听行工具栏
    table.on('tool(LAY-app-content-comm)',function (obj) {
      //获取行数据
      var data = obj.data;
      //点击删除
      if (obj.event === 'del'){
        layer.confirm('确定删除吗？', function(index){
          $.ajax({
            url:'admin/deleteByIdGg',
            data:{"blogGgid":data.blogGgid},
            dataType:'json',
            method:'post',
            success:function (res) {
              if (res.success){
                layer.msg('删除成功', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }else {
                layer.msg('删除失败', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }
              table.reload('LAY-app-content-comm');
            }
            ,error: function(){
              layer.alert('系统错误，请联系管理员！', {
                icon: 2,
                skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
              });
            }
          });
          layer.close(index);
        });
      } else if (obj.event === 'edit'){
          //点击编辑
          layer.open({
            //打开子窗口
            type: 2
            ,title: '编辑评论',
            data:data
            ,content: 'admin/editComment'
            ,area: ['450px', '240px'],
            btn: ['确定', '取消']  ,
            success:function (layero, index) {
              //打开子窗口后通过success赋值,两个变量基本是固定写法
              var body = layer.getChildFrame('body', index);
              var iframeWin = window[layero.find('iframe')[0]['name']];
              //给子窗口中的元素赋值
              body.find('textarea[name="updateComment"]').val(data.commentContent);
            },yes: function(index, layero){
              //模板自带,固定写法,要注意子窗口中的提交按钮id对应submitID
              var iframeWindow = window['layui-layer-iframe'+ index]//得到iframe页的窗口对象，执行iframe页的方法：
                      ,submitID = 'LAY-user-front-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
              var body = layer.getChildFrame('body', index);
              var updateComment = body.find('textarea[name="updateComment"]').val()
              //监听提交
              iframeWindow.layui.form.on('submit('+ submitID +')', function(obj){
                $.ajax({
                  url:'admin/updateComment',
                  data:{"commentId":data.commentId,"updateComment":updateComment},
                  dataType:'json',
                  method:'post',
                  success:function (res) {
                    if (res.success){
                      layer.msg('修改评论成功', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                      });
                    }else {
                      layer.msg('修改评论失败', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                      });
                    }
                    table.reload('LAY-app-content-comm');
                  }
                  ,error: function(){
                    layer.alert('系统错误，请联系管理员！', {
                      icon: 2,
                      skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    });
                  }
                });
                layer.close(index); //关闭弹层
              });
              submit.trigger('click');
            }
          });
      }
    })

    $('.layui-btn.layuiadmin-btn-comm').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>

